package com.example.jdbc.service.impl;

import com.example.jdbc.dao.UserDao;
import com.example.jdbc.pojo.User;
import com.example.jdbc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    /**
     * 直接使用@Transactional注解即可，无需再配置xml或者配置类
     * @param user
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addUser(User user) {
        this.userDao.insertIntoUser(user);
    }

    @Override
    public List<User> getAllUsers() {
        return this.userDao.selectAllUsers();
    }

    @Override
    public User getUserById(int userId) {
        return this.userDao.selectUserByUserId(userId);
    }

    @Override
    @Transactional
    public void updateUserByUserId(User user) {
        this.userDao.updateUserByUserId(user);
    }

    @Override
    @Transactional
    public void deleteUser(int userId) {
        this.userDao.deleteUserById(userId);
    }
}
